<?php
namespace app\appletsapi\controller;

// 公共类

use app\common\service\ServJwt;
use think\Controller;

class Base extends Controller
{
    public $_userid;
    public $_user_name;
    public $limit = 10;
    public $my_appid = 'wx4826361d374b8ab6';
    public $my_appsecret = '747d65f61aec010915c117620851feb5';
    public $pay_key = 'dongmianshichang1234561234561234';
    public $my_secret = '747d65f61aec010915c117620851feb5';

    public function __construct()
    {
        parent::__construct();
        header("Content-type:text/html;charset=utf-8");
//        $this->loadLoginUser();
        $this->isLogin();
    }

    // 验证是否登录
    function isLogin()
    {
        $array = [
            '/api/wxlogin',
            '/api/addpersonalinfo',
            '/api/notify',
            '/api/firstpage',
            '/api/indexshops',
            '/api/products/typelist',
            '/api/products/productslist',
            '/api/findshops',
            '/api/products/productsinfo',
        ];
        $access_token = request()->header('token');
        if(isset($access_token) && !empty($access_token)){
            $jwtServer = new ServJwt();
            $token_test = $jwtServer->verifyToken($access_token);
            // 是否登录
            if(!$token_test['iss']){
                $this->jsonresult('','-1','登录已失效，请重新登录');
            }
            // 判断接口是否过期
            if (time() > $token_test['exp']){
                $this->jsonresult('','-1','登录已失效，请重新登录');
            }
            $this->_userid = $token_test['iss']['user_id'];
            $this->_user_name = $token_test['iss']['user_name'];
            return true;
        }
        $requesturl = request()->baseurl();
        if(in_array($requesturl,$array)){
            return true;
        }
        $this->jsonresult('','-1','请先登录');
    }

//    function getAllHeaders(){//获取请求头
//        $headers = [];
//        foreach ($_SERVER as $name => $value){
//            if (substr($name, 0, 5) == 'HTTP_'){
//                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
//            }
//        }
//        return $headers;
//    }

// 返回身份数据
//    function GetUserInfo()
//    {
//        $param = [
//            "spf_id"    => $this->_spfid,
//            "utype"    => $this->_utype,
//            "shopid"    => $this->_shopid,
//            "userid"    => $this->_userid,
//            "user_name"  => $this->_user_name,
//            "lease"     => $this->_lease,
//        ];
//
//        return $param;
//    }

    public function jscode2session($code)
    {
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->my_appid . "&secret=" . $this->my_appsecret ."&js_code=" . $code . "&grant_type=authorization_code";
//        var_dump($url);
        $rs = $this->http_curl($url);
//        var_dump($rs);die;
        $rs["data"] = [];
        $rs["is_error"] = true;
        if ($rs['status'] == 200) {//正常返回
            $str_arr = json_decode($rs['str'], true);
//            var_dump($str_arr);die;
            if (!empty($str_arr) && (!isset($str_arr['errcode']) || $str_arr['errcode'] == 0)) {
                $rs["data"] = $str_arr;
                $rs["is_error"] = false;
            }
        }
        return $rs;
    }

    protected function jsonresult($data,$code,$msg){
        $msg = mb_convert_encoding( $msg, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        $this->result($data,$code,$msg);
    }

    /**
     * curl post提交
     *
     * @param mixed $url
     * @param mixed $var
     * @param mixed $timeout
     * @param mixed $type
     * @param mixed $referer
     * @return string
     */
    public function http_curl($url, $var="", $theHeaders=array(), $type = 'get', $timeout = 120, $referer = ''){
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//记得注释了
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
        if(!empty($theHeaders)){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $theHeaders);
        }
        if(!empty($referer)){
            curl_setopt($curl, CURLOPT_REFERER, $referer);
        }
        //curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies.txt');
        //curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt');
        if($type == 'post'){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $var);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);

        $data['str'] = curl_exec($curl);
        $data['status'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $data['errno'] = curl_error($curl);

        curl_close($curl);

        return $data;
    }

}
